package edu.xidian.forum.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import edu.xidian.forum.common.Result;
import edu.xidian.forum.dto.BillboardDto;
import edu.xidian.forum.entity.Billboard;
import edu.xidian.forum.service.BillboardService;
import edu.xidian.forum.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import static edu.xidian.forum.jwt.JwtUtil.USER_NAME;

@RestController
@RequestMapping("/billboard")
public class BillboardController {
    @Resource
    private BillboardService billboardService;

    @Resource
    private UserService userService;

    @GetMapping("/show")
    public Result<Billboard> getNotices() {
        List<Billboard> list = billboardService.list(Wrappers.<Billboard>lambdaQuery().eq(Billboard::isShow, true));
        return Result.success(list.get(list.size() - 1));
    }

    /**
     * 前端Dto中只需要content
     */
    @PostMapping("/add")
    public Result<Billboard> addNotices(@RequestHeader(value = USER_NAME) String username, @RequestBody BillboardDto billboardDto) {
        if (!userService.isAdmin(username)) {
            return Result.failed("Admin No valid!");
        }
        return billboardService.insertOne(billboardDto);
    }


}
